package com.briup.jdbc.manager;

import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;

import com.briup.jdbc.conn.JDBCPool;
import com.briup.jdbc.handler.JDBCHandler;

public class SQLSession {

	private Connection conn;

	public SQLSession() throws SQLException, InterruptedException {
		super();
		this.conn = JDBCPool.getConn();
		this.conn.setAutoCommit(false);
	}

	public <T> T getMapper(Class<T> clas)
			throws IllegalArgumentException, InstantiationException, IllegalAccessException {
		return (T) Proxy.newProxyInstance(clas.getClassLoader(), new Class[] { clas }, new JDBCHandler(conn, clas));
	}

	public void commit() throws SQLException {
		JDBCPool.commit(conn);
		System.out.println(">>>手动提交>>>");
	}

	public void rollback() throws SQLException {
		JDBCPool.rollback(conn);
		System.out.println(">>>手动回滚>>>");
	}

	public void close() throws SQLException, InterruptedException {
		JDBCPool.close(conn);
		System.out.println(">>>手动关闭>>>");
	}

}
